package com.zj.z11select.controller;


import com.zj.z11select.entity.User;
import com.zj.z11select.secruity.domian.LoginBody;
import com.zj.z11select.service.UserService;
import com.zj.z11select.vo.BaseResult;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户信息表  前端控制器
 * </p>
 *
 * @author ZHOUJIE
 * @since 2021-07-06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/client/user")
@CrossOrigin
@Slf4j
public class UserController {

    private final UserService userService;


    @ApiOperation("用户登录")
    @PostMapping("/login")
    public BaseResult login(@RequestBody LoginBody loginBody){
        try {
           return BaseResult.success(userService.login(loginBody));
        } catch (Exception e) {
            log.error(">login>error：",e);
            return BaseResult.error(e.getMessage());
        }
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public BaseResult register(@RequestBody User user){
        try {
            User registerUser = this.userService.register(user.getUsername(), user.getPassword());
            if (!ObjectUtils.isEmpty(registerUser)){
                return BaseResult.success("注册成功",registerUser);
            }
        } catch (Exception e){
            log.error(">register>error：",e);
            return BaseResult.error(e.getMessage());
        }
        return BaseResult.error("注册失败");
    }

    @ApiOperation("用户名是否存在")
    @GetMapping("/checkUser")
    public BaseResult checkUser(String username){
        try {
            return this.userService.checkUser(username) ? BaseResult.error("用户名已存在") : BaseResult.success();
        } catch (Exception e) {
            log.error(">checkUser>error：",e);
            return BaseResult.error(e.getMessage());
        }
    }

}

